package com.demo.controller;

import com.demo.bean.Task;
import com.demo.bean.User;
import com.demo.service.EventManageService;
import com.demo.service.RedisService;
import com.demo.service.TaskService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import static com.demo.Tools.DateCompute.*;

/**
 * @author 李天航
 * 活动的增删改查和活动安排
 */
@Api(tags = "任务")
@RestController
@RequestMapping("/task")
public class TaskController {

    @Resource
    EventManageService eventManageService;
    @Resource
    TaskService taskService;
    @Resource
    RedisService redisService;

    @PostMapping("/manage")
    public LinkedHashMap<String, Object> taskManage(HttpSession session,long weekNum,String algorithmName)
    {
        if(weekNum<getNowWeekNum())
        {
            LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
            resultMap.put("status",500);
            resultMap.put("msg","不安排过去的活动哦");
            return resultMap;
        }
        User user = (User)  redisService.getSessionData(session.getId(),"user");
        if(algorithmName==null){
            algorithmName="loadBalanceFirst";
        }
        return eventManageService.eventManage(user.getUserAccount(),weekNum,algorithmName);
    }

    /**
     * 添加任务
     * @param task 任务信息
     * @param session 缓存
     * @return status:状态编码 msg:信息
     */
    @PostMapping("/add")
    public Object addTask(Task task,HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        task.setUserAccount(user.getUserAccount());
        return taskService.addTask(task);
    }

    /**
     * 删除一项任务
     * @param taskId 任务编号
     * @return status:状态编码 msg:信息
     */
    @PostMapping("/delete")
    public Object deleteTask(int taskId,HttpSession session)
    {
        User user=(User)redisService.getSessionData(session.getId(),"user");
        return taskService.deleteTask(taskId,user.getUserAccount());
    }

    /**
     * 获取任务列表
     * @param weekNum 周数
     * @param session 缓存
     * @return status:操作状态 msg:信息 tasks:任务列表
     */
    @PostMapping("/getall")
    public Object getTasks(Long weekNum,HttpSession session)
    {
        User user=(User)redisService.getSessionData(session.getId(),"user");
        return taskService.getTasks(new Task(user.getUserAccount(),weekNum));
    }

    /**
     * 将finishTag置为1
     * @param taskId 任务编号
     * @return status:操作状态 msg:信息
     */
    @PostMapping("/finish")
    public Object finishTask(int taskId)
    {
        return taskService.finishTask(taskId);
    }

    /**
     * 修改任务
     * @param task 修改后的任务
     * @param session 缓存
     * @return status,msg
     */
    @PostMapping("/update")
    public Object updateTask(Task task,HttpSession session)
    {
        User user=(User)redisService.getSessionData(session.getId(),"user");
        task.setUserAccount(user.getUserAccount());
        return taskService.updateTask(task);
    }

    /**
     * 最大间隔时间(根据固定活动，每周活动，日常活动)
     * @param weekNum 周数
     * @param session 缓存
     * @return status,msg,intervalTime
     */
    @PostMapping("/max/interval")
    public Object maxIntervalTimeFromFixedEvent(Long weekNum,HttpSession session)
    {
        LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
        try {
            User user = (User)redisService.getSessionData(session.getId(),"user");
            Long intervalTime=eventManageService.maxTaskTimeFromFixedEvents(user.getUserAccount(),weekNum);
            if(intervalTime!=-1)
            {
                resultMap.put("status",200);
                resultMap.put("msg","查找成功");
                resultMap.put("intervalTime",intervalTime);
            }
            else
            {
                resultMap.put("status",500);
                resultMap.put("msg","没有足够的活动");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg","查找失败");
        }
        return resultMap;
    }

    /**
     * 根据最大时间间隔(根据固定活动，每周活动，睡觉)
     * @param weekNum 周数
     * @param session 缓存
     * @return status,msg,intervalTime
     */
    @PostMapping("/max/interval/ignore")
    public Object maxIntervalIgnoreDailyactivities(Long weekNum,HttpSession session)
    {
        LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
        try {
            User user =(User)redisService.getSessionData(session.getId(),"user");
            Long intervalTime=eventManageService.maxTaskIgnoreDailyActivities(user.getUserAccount(),weekNum);
            if(intervalTime!=-1)
            {
                resultMap.put("status",200);
                resultMap.put("msg","查找成功");
                resultMap.put("intervalTime",intervalTime);
            }
            else
            {
                resultMap.put("status",500);
                resultMap.put("msg","没有足够的活动");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            resultMap.put("status",500);
            resultMap.put("msg","查找失败");
        }
        return resultMap;
    }
}
